home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / cpp-kit.lha / c++kit / Parse.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  4KB  |  223 lines

  1. #include "Parse.H"
  2.  
  3. Parse::Parse(char * file)
  4. :  _state(And), _mark(_marks), _retn(0)
  5. {
  6.    _scan = new Scan(file);
  7.    *_mark = _scan->mark();
  8.    _mark++;
  9. }
  10.  
  11. void
  12. Parse::open(char * file)
  13. {
  14.    _state = And;
  15.    _mark = _marks;
  16.    _retn = 0;
  17.    _scan = new Scan(file);
  18.    *_mark = _scan->mark();
  19.    _mark++;
  20. }
  21.  
  22. Parse::Parse(Scan& scan)
  23. :  _state(And), _mark(_marks), _retn(0)
  24. {
  25.    _scan = &scan;
  26.    *_mark = _scan->mark();
  27.    _mark++;
  28. }
  29.  
  30. void
  31. Parse::open(Scan& scan)
  32. {
  33.    _state = And;
  34.    _mark = _marks;
  35.    _retn = 0;
  36.    _scan = &scan;
  37.    *_mark = _scan->mark();
  38.    _mark++;
  39. }
  40.  
  41. Parse&
  42. Parse::operator , (char c)
  43. {
  44. Token t;
  45.  
  46.    if( _state == And ) {
  47.       if( _scan->match(c, t) == 0 ) {
  48.      _state = Fail;
  49.       }
  50.    }
  51.    return *this;
  52. }
  53.  
  54. Parse&
  55. Parse::operator , (char * s)
  56. {
  57. Token t;
  58.    if( _state == And ) {
  59.       if( _scan->match(s, t) == 0 ) {
  60.      _state = Fail;
  61.       }
  62.    }
  63.    return *this;
  64. }
  65.  
  66. Parse&
  67. Parse::operator , (Parse& (*func)(Parse&) )
  68. {
  69.    return func(*this);
  70. }
  71.  
  72. Parse&
  73. OR(Parse& P)
  74. {
  75.    if( P._state == Parse::Fail ) {
  76.       P._state = Parse::And;
  77.       P._mark--;
  78.       P._scan->back(*P._mark);
  79.    }
  80.    else if( P._state == Parse::And ) {
  81.       P._state = Parse::Succ;
  82.    }
  83.    return P;
  84. }
  85.  
  86. Parse&
  87. Parse::operator , (const NUMBER& token)
  88. {
  89.    if( _state == And ) {
  90.       if( !_scan->number(*(token._token)) ) {
  91.      _state = Fail;
  92.       }
  93.    }
  94.    return *this;
  95. }
  96.  
  97. Parse&
  98. Parse::operator , (const STRING& token)
  99. {
  100.    if( _state == And ) {
  101.       if( !_scan->string(*(token._token)) ) {
  102.      _state = Fail;
  103.       }
  104.    }
  105.    return *this;
  106. }
  107.  
  108. Parse&
  109. Parse::operator , (const IDENT& token)
  110. {
  111.    if( _state == And ) {
  112.       if( !_scan->identifier(*(token._token)) ) {
  113.      _state = Fail;
  114.       }
  115.    }
  116.    return *this;
  117. }
  118.  
  119. Parse&
  120. Parse::operator , (const TOKEN& token)
  121. {
  122.    if( _state == And ) {
  123.       if( !_scan->token(*(token._token)) ) {
  124.      _state = Fail;
  125.       }
  126.    }
  127.    return *this;
  128. }
  129.  
  130. Parse&
  131. Parse::operator , (const CHARAC& token)
  132. {
  133.    if( _state == And ) {
  134.       if( !_scan->character(*(token._token)) ) {
  135.      _state = Fail;
  136.       }
  137.    }
  138.    return *this;
  139. }
  140.  
  141. Parse&
  142. Parse::operator , (const CHOOSE& choice)
  143. {
  144.    if( _state == And ) {
  145.       _retn = choice._retn;
  146.    }
  147.    return *this;
  148. }
  149.  
  150. Parse&
  151. EOFL(Parse& P)
  152. {
  153.    if( P._state == Parse::And ) {
  154.       if(  !P._scan->eof() ) {
  155.      P._state = Parse::Fail;
  156.       }
  157.    }
  158.    return P;
  159. }
  160.  
  161. Parse&
  162. Parse::operator , (const MATCH0& M0)
  163. {
  164.    if( _state == And) {
  165.    int (*func)(Parse&) = M0._func;
  166.       *_mark = _scan->mark();
  167.       _mark++;
  168.       if( (func)(*this) == 0 ) {
  169.      _state = Fail;
  170.       }
  171.       _mark--;
  172.    }
  173.    return *this;
  174. }
  175.  
  176. template <class T> Parse&
  177. operator , (Parse& P, const MATCH1<T>& M1)
  178. {
  179.    if( P._state == Parse::And) {
  180.    int (*func)(Parse&, T&) = M1._func;
  181.       *P._mark = P._scan->mark();
  182.       P._mark++;
  183.       if( (func)(P, M1._value) == 0 ) {
  184.      P._state = Parse::Fail;
  185.       }
  186.       P._mark--;
  187.    }
  188.    return P;
  189. }
  190.  
  191. template <class S, class T> Parse&
  192. operator , (Parse& P, const MATCH2<S, T>& M2)
  193. {
  194.    if( P._state == Parse::And) {
  195.    int (*func)(Parse&, S&, T&) = M2._func;
  196.       *P._mark = P._scan->mark();
  197.       P._mark++;
  198.       if( (func)(P, M2._value1, M2._value2) == 0 ) {
  199.      P._state = Parse::Fail;
  200.       }
  201.       P._mark--;
  202.    }
  203.    return P;
  204. }
  205.  
  206. MATCH0
  207. MATCH( int (*func)(Parse&))
  208. {
  209.    return MATCH0(func);
  210. }
  211.  
  212. template<class T> MATCH1<T>
  213. MATCH(int (*func)(Parse&, T&), T& value1)
  214. {
  215.    return MATCH1<T>(func, value1);
  216. }
  217.  
  218. template<class S, class T> MATCH2<S, T>
  219. MATCH(int (*func)(Parse&, S&, T&), S& value1, T& value2)
  220. {
  221.    return MATCH2<S,T>(func, value1, value2);
  222. }
  223.